package com.pinyoumei.app.fragment;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.pinyoumei.app.R;
import com.pinyoumei.app.view.OnTabSelectListener;
import com.pinyoumei.app.view.SlidingTabLayout;

import java.util.ArrayList;


@SuppressLint("ValidFragment")
public class ThirdFragment extends BaseLazyLoadFragment {

    private String[] tabList = {"二维码分享", "素材库"};
    private SlidingTabLayout tl_5;
    private ArrayList<BaseLazyLoadFragment> mFragments = new ArrayList<>();
    private ViewPager vp;
    private String mTitle;
    public static ThirdFragment getInstance(String title) {
        ThirdFragment sf = new ThirdFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_third;
    }

    @Override
    public void initView(View rootView) {

            mFragments.add(new ShareQrCodeFragment());
            mFragments.add(new MarketingMaterialFragment());

        tl_5 = rootView.findViewById(R.id.tl_5);
        vp = rootView.findViewById(R.id.vp);
        MyPagerAdapter mAdapter = new MyPagerAdapter(getChildFragmentManager());
        vp.setAdapter(mAdapter);
        tl_5.setViewPager(vp);

        tl_5.getTitleView(0).setTypeface(Typeface.DEFAULT_BOLD);

        tl_5.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                for (int i = 0; i < tl_5.getTabCount(); i++) {
                    if (position == i) {
                        //选中的文字
                        tl_5.getTitleView(position).setTypeface(Typeface.DEFAULT_BOLD);
                    } else {
                        //未选中的文字，并且设置未选中时的不加粗
                        tl_5.getTitleView(i).setTypeface(Typeface.DEFAULT);
                    }
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabList[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}